package com.smart.cosstarter.model;

import com.smart.cosstarter.utils.FileUtils;

import java.io.*;
import java.util.Scanner;

/**
 * @Author: jj
 * @Date: 2019/8/22 17:52
 * @Version 1.0
 */
public class DownloadObject {

	private String bucket;

	private String key;

	private String absolutePath;

	private byte[] bytes;

	private String fileName;

	protected DownloadObject(File file, String bucket, String key) {
		this.bytes = FileUtils.getBytesByFile(file);
		this.absolutePath = file.getAbsolutePath();
		this.bucket = bucket;
		this.key = key;
		this.fileName = file.getName();
	}

	protected DownloadObject(String absolutePath, byte[] bytes, String bucket, String key) {
		this.bytes = bytes;
		this.absolutePath = absolutePath;
		this.bucket = bucket;
		this.key = key;
		this.fileName = FileUtils.getFileByBytes(bytes, absolutePath).getName();
	}

	protected DownloadObject(String filePath,String fileName, byte[] bytes, String bucket, String key) {
		this.bytes = bytes;
		this.absolutePath = filePath + System.getProperty("file.separator") + fileName;
		this.bucket = bucket;
		this.key = key;
		this.fileName = fileName;
	}

	public File getFile(String absolutePath) {
		return FileUtils.getFileByBytes(bytes, absolutePath);
	}

	public File getFile(String filePath,String fileName) {
		return FileUtils.getFileByBytes(bytes, filePath + System.getProperty("file.separator") + fileName);
	}

	public File getFile() {
		return FileUtils.getFileByBytes(bytes, absolutePath);
	}

	protected void setFile(File file) {
		this.bytes = FileUtils.getBytesByFile(file);
		this.absolutePath = file.getAbsolutePath();
		this.fileName = file.getName();
	}

	public String getAbsolutePath() {
		return absolutePath;
	}

	public void setAbsolutePath(String absolutePath) {
		this.absolutePath = absolutePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getBucket() {
		return bucket;
	}

	protected void setBucket(String bucket) {
		this.bucket = bucket;
	}

	public String getKey() {
		return key;
	}

	protected void setKey(String key) {
		this.key = key;
	}

	public Scanner getReader() {
		try {
			return new Scanner(getFile());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Scanner getReader(String charset) {
		try {
			return new Scanner(getFile(), charset);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public InputStream getStream() {
		try {
			return new FileInputStream(getFile());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public byte[] convertBytes() {
		return bytes;
	}

	public void write(OutputStream os) {
		try {
			os.write(bytes);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public FileWriter toWriter() {
		try {
			return new FileWriter(getFile());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


}

